package com.ruoyi.docdraw.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.docdraw.domain.SysProject;
import com.ruoyi.docdraw.domain.subProject.SysSubProjectVo;
import com.ruoyi.docdraw.service.*;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.docdraw.domain.SysSubProject;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目子项目协作Controller
 * 
 * @author lvtian
 * @date 2025-04-14
 */
@RestController
@RequestMapping("/docdraw/subProject")
public class SysSubProjectController extends BaseController
{
    @Autowired
    private ISysSubProjectService sysSubProjectService;

    @Autowired
    private ISysGDPService sysGDPService;

    @Autowired
    private ISysProjectService sysProjectService;

    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private ISysSupplierService sysSupplierService;
    @Autowired
    private ISysProjectSupplierService sysProjectSupplierService;

    /**
     * 查询项目子项目协作列表
     */
    @PreAuthorize("@ss.hasPermi('docdraw:subProject:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysSubProject sysSubProject)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        startPage();
        List<SysSubProject> list = sysGDPService.getSubProject(sysSubProject,user);
        List<SysSubProjectVo> vos = new ArrayList<>();
        for (SysSubProject subProject : list) {
            vos.add(sysGDPService.getSysSubProjectVo(subProject));
        }
        return getDataTable(vos);
    }

    /**
     * 导出项目子项目协作列表
     */
    @PreAuthorize("@ss.hasPermi('docdraw:subProject:export')")
    @Log(title = "项目子项目协作", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysSubProject sysSubProject)
    {
        List<SysSubProject> list = sysSubProjectService.selectSysSubProjectList(sysSubProject);
        List<SysSubProjectVo> vos = new ArrayList<>();
        for (SysSubProject subProject : list) {
            vos.add(sysGDPService.getSysSubProjectVo(subProject));
        }
        ExcelUtil<SysSubProjectVo> util = new ExcelUtil<SysSubProjectVo>(SysSubProjectVo.class);
        util.exportExcel(response, vos, "项目子项目协作数据");
    }

    /**
     * 获取项目子项目协作详细信息
     */
    @PreAuthorize("@ss.hasPermi('docdraw:subProject:query')")
    @GetMapping(value = "/{subProjectId}")
    public AjaxResult getInfo(@PathVariable("subProjectId") Long subProjectId)
    {
        return success(sysSubProjectService.selectSysSubProjectBySubProjectId(subProjectId));
    }

    /**
     * 新增项目子项目协作
     */
    @PreAuthorize("@ss.hasPermi('docdraw:subProject:add')")
    @Log(title = "项目子项目协作", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysSubProject sysSubProject)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();

        Long projectId = sysSubProject.getProjectId();
        if(ObjectUtil.isNull(projectId)){
            return error("父项目参数缺失请填写");
        }
        SysProject sysProject = sysProjectService.selectSysProjectByProjectId(projectId);
        String status = sysProject.getStatus();
        if(UserConstants.USER_DISABLE.equals(status)){
            Long projectDeptId = sysProject.getProjectDeptId();
            SysDept sysDept = sysDeptService.selectDeptById(projectDeptId);
            return error("项目 ["+sysProject.getProjectName()+"] 已被暂停 请联系 组织 "+sysDept.getDeptName()+" 的管理员");
        }
        if (sysSupplierService.checkUserIsSupplier(user)) {
            // 如果是受邀的供应商创建
            sysSubProject.setSubProjectSupplierId(user.getDeptId());
            // 设置供应商项目经理的id
        }

        if (!sysSubProjectService.checkSubProjectNameUnique(sysSubProject)) {
            return error("当前项目下协作子项目名称已存在 请检查");
        }
        sysSubProject.setCreateBy(user.getUserName());
        return toAjax(sysSubProjectService.insertSysSubProject(sysSubProject));
    }

    /**
     * 修改项目子项目协作
     */
    @PreAuthorize("@ss.hasPermi('docdraw:subProject:edit')")
    @Log(title = "项目子项目协作", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysSubProject sysSubProject)
    {

        Long projectId = sysSubProject.getProjectId();
        if(ObjectUtil.isNull(projectId)){
            return error("父项目参数缺失请填写");
        }
        SysProject sysProject = sysProjectService.selectSysProjectByProjectId(projectId);
        String status = sysProject.getStatus();
        if(UserConstants.USER_DISABLE.equals(status)){
            Long projectDeptId = sysProject.getProjectDeptId();
            SysDept sysDept = sysDeptService.selectDeptById(projectDeptId);
            return error("项目 ["+sysProject.getProjectName()+"] 已被暂停 请联系 组织 "+sysDept.getDeptName()+" 的管理员");
        }

        if (!sysSubProjectService.checkSubProjectNameUnique(sysSubProject)) {
            return error("当前项目下协作子项目名称已存在 请检查");
        }
        sysSubProject.setUpdateBy(getUsername());
        return toAjax(sysSubProjectService.updateSysSubProject(sysSubProject));
    }

    /**
     * 删除项目子项目协作
     */
    @PreAuthorize("@ss.hasPermi('docdraw:subProject:remove')")
    @Log(title = "项目子项目协作", businessType = BusinessType.DELETE)
	@DeleteMapping("/{subProjectIds}")
    public AjaxResult remove(@PathVariable Long[] subProjectIds)
    {
        return toAjax(sysSubProjectService.deleteSysSubProjectBySubProjectIds(subProjectIds));
    }
}
